package com.google.code.sip.ucenter.web.admin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.core.utils.WebGridUtils;
import com.google.code.sip.ucenter.model.Account;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.AccountManager;
import com.google.code.sip.ucenter.service.UserManager;

@Controller
public class HuoshowAccountController {

	@Autowired
	private AccountManager huoshowAccountManager;

	@Autowired
	private UserManager userManager;

	@RequestMapping("/admin/huoshowAccount.do")
	public ModelAndView list(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String, Object>();
		Page<Account> page = new Page<Account>();
		WebGridUtils.fillPage(request, page);

		String username = request.getParameter("username");
		String email = request.getParameter("email");
		String huoshowId = request.getParameter("huoshowId");
		boolean hasUserName = StringUtils.isNotBlank(username);
		boolean hasEmail = StringUtils.isNotBlank(email);
		boolean hasHuoshowId = StringUtils.isNotBlank(huoshowId);
		User user = null;
		if (hasUserName || hasEmail || hasHuoshowId) {
			user = userManager.getUser(username.trim(), email.trim(), huoshowId
					.trim());
			if (user != null) {
				page.addFilter("user", user);
				page = huoshowAccountManager.findLogByPage(page);
				model.put("totalRows", (int) page.getTotalCount());
			} else {
				model.put("totalRows", 0);
			}
		} else {
			page = huoshowAccountManager.findLogByPage(page);
			model.put("totalRows", (int) page.getTotalCount());
		}
		// page = huoshowAccountManager.findByPage(page);
		// model.put("totalRows", (int) page.getTotalCount());
		model.put("page", page);
		return new ModelAndView("admin.huoshowAccount.list", model);
	}
}
